﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ETS_FinancialData_sync_toolsForServer
{
    public partial class setup : Form
    {
        public setup()
        {
            InitializeComponent();
        }
        //数据库连接
        SQLiteConnection m_dbConnection;
        SQLiteDataAdapter da;
        DataSet ds;
        //创建一个连接到指定数据库
        void connectToDatabase()
        {
            m_dbConnection = new SQLiteConnection("Data Source=123.sqlite;Version=3;");
            m_dbConnection.Open();
        }
        private void showDataGV()
        {
            SQLiteCommand cmd = new SQLiteCommand(m_dbConnection);//实例化SQL命令
            cmd.CommandText = "select * from DataReplaceRules";
            //DataSet ds=SQLiteHelper.ExecuteDataset(cmd);
            ds = new DataSet();
            da = new SQLiteDataAdapter(cmd);
            da.Fill(ds);
            //da.Dispose();
            cmd.Connection.Close();
            //cmd.Dispose();
            dataGridView1.DataSource = ds.Tables[0];
        }
        helper h;
        private void setup_Load(object sender, EventArgs e)
        {
            connectToDatabase();
            showDataGV();
            dataGridView1.RowHeadersDefaultCellStyle.Padding = new Padding(15);

            h= new helper();
            txtboxIP.Text = h.ContentValue("Connection", "ServerName");
            txtBoxDBname.Text = h.ContentValue("Connection", "Database");
            txtBoxUserid.Text = h.ContentValue("Connection", "LogId");
            txtBoxPSW.Text = h.ContentValue("Connection", "Logpass");
            txtPort.Text = h.ContentValue("Connection", "port");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                SQLiteCommandBuilder bu = new SQLiteCommandBuilder(da);
                da.Update(ds, "Table");
                MessageBox.Show("保存成功！");
                showDataGV();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows[dataGridView1.Rows.Count-1].Selected==true)
                {
                    MessageBox.Show("空行不可删除！");
                    return;
                }
                connectToDatabase();
                SQLiteCommand cmd = new SQLiteCommand(m_dbConnection);//实例化SQL命令
                cmd.CommandText = "delete from DataReplaceRules where id=" + dataGridView1.SelectedRows[0].Cells["id"].Value;
                cmd.ExecuteNonQuery();
                showDataGV();
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            h.WriteINI("ServerName", txtboxIP.Text);
            h.WriteINI("Database",txtBoxDBname.Text);
            h.WriteINI("LogId", txtBoxUserid.Text);
            h.WriteINI("Logpass", txtBoxPSW.Text);
            h.WriteINI("port", txtPort.Text);
            MessageBox.Show("保存成功！");
        }
    }
}
